package com.lb.jwitter.action;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import com.lb.jwitter.service.ConditionReport;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXADMNhom;
import com.lb.jwitter.service.TblQLDTTXADMNhomService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLai;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiBienLaiService;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiMonHocService;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhi;
import com.lb.jwitter.service.TblQLDTTXAQuiDinhHocPhiService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;

import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;

import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALopMonHocEditFormAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private int id; 
    public int getId() { return id; }

    public void setId(int id) { this.id = id; }

    private TblQLDTTXALopMonHoc tblQLDTTXALopMonHoc; 
    public TblQLDTTXALopMonHoc getTblQLDTTXALopMonHoc() { 
        return tblQLDTTXALopMonHoc; 
    } 
    private Collection<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs; 
    public Collection<TblQLDTTXALopMonHoc>  getTblQLDTTXALopMonHocs() {
         return tblQLDTTXALopMonHocs; 
      } 

    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService; 
    private TblQLDTTXAQuiDinhHocPhiService tblQLDTTXAQuiDinhHocPhiService;
    private TblQLDTTXAHocVienHocPhiBienLaiService tblQLDTTXAHocVienHocPhiBienLaiService;

    private Collection<TblQLDTTXAMonHoc> tblQLDTTXAMonHocs; 
    public Collection<TblQLDTTXAMonHoc>  getTblQLDTTXAMonHocs(){ 
              return tblQLDTTXAMonHocs; 
     } 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

    private Collection<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public Collection<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private Collection<TblQLDTTXAGiaoVien> tblQLDTTXAGiaoViens; 
    public Collection<TblQLDTTXAGiaoVien>  getTblQLDTTXAGiaoViens(){ 
              return tblQLDTTXAGiaoViens; 
     } 

    private TblQLDTTXAGiaoVienService tblQLDTTXAGiaoVienService; 
    
    private Collection<TblQLDTTXADMNhom> tblQLDTTXADMNhoms; 
    public Collection<TblQLDTTXADMNhom>  getTblQLDTTXADMNhoms(){ 
              return tblQLDTTXADMNhoms; 
     } 

    private TblQLDTTXADMNhomService tblQLDTTXADMNhomService; 

    private Collection<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens; 
    public Collection<TblQLDTTXALopMonHocHocVien>  getTblQLDTTXALopMonHocHocViens(){ 
              return tblQLDTTXALopMonHocHocViens; 
     } 
    
    private Collection<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocVienThis; 
    public Collection<TblQLDTTXALopMonHocHocVien>  getTblQLDTTXALopMonHocHocVienThis(){ 
              return tblQLDTTXALopMonHocHocVienThis; 
     } 

    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService; 

    private Collection<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public Collection<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 
    private TblQLDTTXAHocVienHocPhiMonHocService tblQLDTTXAHocVienHocPhiMonHocService;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
       try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "sua")){ 
        return "noprivilege"; 
    }
        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient"); 
        
        
//        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 
//        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        
        tblQLDTTXAGiaoVienService = (TblQLDTTXAGiaoVienService) context.getBean("tbl_QLDTTXA_GiaoVienClient"); 
        tblQLDTTXADMNhomService = (TblQLDTTXADMNhomService) context.getBean("tbl_QLDTTXA_DM_NhomClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
        tblQLDTTXAHocVienHocPhiMonHocService = (TblQLDTTXAHocVienHocPhiMonHocService) context.getBean("tbl_QLDTTXA_HocVienHocPhi_MonHocClient");
        tblQLDTTXAQuiDinhHocPhiService = (TblQLDTTXAQuiDinhHocPhiService) context.getBean("tbl_QLDTTXA_QuiDinhHocPhiClient"); 
        tblQLDTTXAHocVienHocPhiBienLaiService = (TblQLDTTXAHocVienHocPhiBienLaiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhi_BienLaiClient"); 
        
        TblQLDTTXAQuiDinhHocPhi tblQLDTTXAQuiDinhHocPhi = tblQLDTTXAQuiDinhHocPhiService.gettblQLDTTXAQuiDinhHocPhi(new Long(1));
	      
       
        tblQLDTTXALopMonHoc = tblQLDTTXALopMonHocService.gettblQLDTTXALopMonHoc(new Long(id)); 

//        tblQLDTTXAMonHocs = tblQLDTTXAMonHocService.findAlltblQLDTTXAMonHoc(0, 0);
//        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam(0, 0);
        tblQLDTTXAGiaoViens = tblQLDTTXAGiaoVienService.findAlltblQLDTTXAGiaoVien(0, 0);
        
        tblQLDTTXADMNhoms = tblQLDTTXADMNhomService.findAlltblQLDTTXADMNhom(0, 0);
        tblQLDTTXALopMonHocHocVienThis = new ArrayList<TblQLDTTXALopMonHocHocVien>();

   if (pageSize == 0) 
   { 
       pageSize = 20; 
   } 
   if (page == 0) 
   { 
       page = 1; 
   } 
   List<ConditionReport> cons = new ArrayList<ConditionReport>(); 

   cons.clear(); 
   if (id != 0) 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("lopMonHocId.id"); 
       con.setValue1(new Long(id)); 
       con.setOperator("eq"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("trangThai"); 
       con.setValue1(2); 
       con.setOperator("ne"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("nhomId.ten"); 
       con.setValue1("asc"); 
       con.setOperator("order"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("hocVienId.ten"); 
       con.setValue1("asc"); 
       con.setOperator("order"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("hocVienId.hoTen"); 
       con.setValue1("asc"); 
       con.setOperator("order"); 
       cons.add(con); 
   } 
   tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);//pageSize, page);
   
   cons.clear(); 
   if (id != 0) 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("lopMonHocId.id"); 
       con.setValue1(new Long(id)); 
       con.setOperator("eq"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("trangThai"); 
       con.setValue1(2); 
       con.setOperator("ne"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("nhomId"); 
       con.setOperator("isNull"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("hocVienId.ten"); 
       con.setValue1("asc"); 
       con.setOperator("order"); 
       cons.add(con); 
   } 
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("hocVienId.hoTen"); 
       con.setValue1("asc"); 
       con.setOperator("order"); 
       cons.add(con); 
   } 
   tblQLDTTXALopMonHocHocViens.addAll(tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0));
   
   cons.clear();
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("hocVienHocPhiId.hocKyId.id"); 
       con.setValue1(tblQLDTTXALopMonHoc.getHocKyId().getId()); 
       con.setOperator("eq"); 
       cons.add(con); 
   } 
   /*{ 
       ConditionReport con = new ConditionReport(); 
       con.setCol("daDongHP"); 
       con.setValue1(false); 
       con.setOperator("eq"); 
       cons.add(con); 
   }*/ 
   List<TblQLDTTXAHocVienHocPhiMonHoc> hpmhs = tblQLDTTXAHocVienHocPhiMonHocService.findtblQLDTTXAHocVienHocPhiMonHocByCondition(cons, 0, 0);
   
   cons.clear();
   { 
       ConditionReport con = new ConditionReport(); 
       con.setCol("hocKyId.id"); 
       con.setValue1(tblQLDTTXALopMonHoc.getHocKyId().getId()); 
       con.setOperator("eq"); 
       cons.add(con); 
   } 
   List<TblQLDTTXAHocVienHocPhiBienLai> bienlais = tblQLDTTXAHocVienHocPhiBienLaiService.findtblQLDTTXAHocVienHocPhiBienLaiByCondition(cons, 0, 0);
   
   for(TblQLDTTXALopMonHocHocVien lopMonHocHV : tblQLDTTXALopMonHocHocViens){
	   Double hocPhi = 0.0;
	   Double tienDaDong = 0.0;
	   if(hpmhs != null && hpmhs.size() > 0){
		   for(TblQLDTTXAHocVienHocPhiMonHoc hpmh : hpmhs){
			   if(hpmh.getHocVienHocPhiId().getHocVienId().getId().equals(lopMonHocHV.getHocVienId().getId())){
				   if(!hpmh.isMienMon()){
					   hocPhi += hpmh.getThanhTien() + tblQLDTTXAQuiDinhHocPhi.getTienHaTang();
				   }
			   }
		   }
	   }
	   if(bienlais != null && bienlais.size() > 0){
		   for(TblQLDTTXAHocVienHocPhiBienLai bienlai :bienlais){
			   if(bienlai.getHocVienId() != null && bienlai.getHocVienId().getId().equals(lopMonHocHV.getHocVienId().getId())){
				   tienDaDong += bienlai.getSoTien();
			   }
		   }
	   }
	   if(tienDaDong < hocPhi){
		   //System.out.println(tienDaDong.toString() + " " + hocPhi.toString());
		   continue;
	   }
	   if(lopMonHocHV.getTrangThai() == 3){
		   continue;
	   }
	   tblQLDTTXALopMonHocHocVienThis.add(lopMonHocHV);
   }
   
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi EditForm tbl_QLDTTXA_LopMonHoc Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 } 
 } 
 
